package com.example.hellodemo.json;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.hellodemo.R;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonActivity extends AppCompatActivity {
    private TextView tv_metadata;
    private String metaDataStr;
    private TextView tv_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_json);

        tv_metadata = findViewById(R.id.tv_metadata);
        tv_data = findViewById(R.id.tv_data);
        text();
    }

    public  void text(){
        // TODO: 2020/3/17 一、 使用原生方式解析
        test1();     // 1. 使用原生方式---将String转换为JOSNObject
        test2();     //2.  使用原生方式：将String转换为JOSNArray
        test3();     //2.2 开始转换数据　

        // TODO: 2020/3/17 二、 使用GSON解析JSON数据

        gsonTest1();

    }

    private  void test1(){
        // 1.1 准备元数据
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name","小米");
            jsonObject.put("age",1);
            JSONObject familyJson = new JSONObject();
            familyJson.put("father","米爸");
            familyJson.put("mother","米妈");
            jsonObject.put("family",familyJson);

            JSONArray hobbyArray = new JSONArray();
            hobbyArray.put("篮球");
            hobbyArray.put("看书");
            jsonObject.put("hobby",hobbyArray);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        tv_metadata.setText(jsonObject.toString());
    }


    private void test2(){
        JSONArray array = new JSONArray();
        for (int i = 0 ; i < 2; i++){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("name","小米" + i);
                jsonObject.put("age" , 22 + i);
                array.put(jsonObject);

            } catch (JSONException e){
                e.printStackTrace();
            }
        }
        metaDataStr = array.toString();
        tv_metadata.setText(metaDataStr);
    }

    private void test3(){

        findViewById(R.id.btn_String2JOSNArray).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String resultStr = "";
                try {
                    JSONArray jsonArray = new JSONArray(metaDataStr);
                    for (int i = 0; i < jsonArray.length(); i++){
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        resultStr = resultStr + "第" + i + "组姓名" + jsonObject.getString("name") + ",年龄:" + jsonObject.getString("age") + "\n";
                    }
                    tv_data.setText(resultStr);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        });

    }

    //2.1 准备元数据
//使用Gson方式：List2String

    private  void gsonTest1(){

        // 序列化  对象转为json数据

        User user = new User(
                "codeYY",
                "yangyuhe@yy.com",
                 28,
                 true
        );
        Gson gson = new Gson();
        String userJson = gson.toJson(user);

        Log.d("序列化：",userJson);

        // 反序列化
        String userJson2 = "{'isDeveloper':false,'name':'xiaoqiang','age':26,'email':'578570174@qq.com'}";
        User user1 = gson.fromJson(userJson2,User.class);
        Log.d("反序列化：","" + user1);


    }





        static class  User{
         private String name;
         private String email;
         private int age;
         private  boolean isDeveloper;


            public User(String name, String email, int age, boolean isDeveloper) {
                this.name = name;
                this.email = email;
                this.age = age;
                this.isDeveloper = isDeveloper;
            }


        }







}
